package com.sky.controller.user;

import com.alibaba.fastjson.JSON;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.User;
import com.sky.exception.PasswordErrorException;
import com.sky.properties.JwtProperties;
import com.sky.properties.WeChatProperties;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import com.sky.service.UserService;
import com.sky.utils.HttpClientUtil;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(value = "地址簿相关接口")
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;

    @GetMapping("/list")
    @ApiOperation("获取当前用户地址")
    public Result<List<AddressBook>> list(){
       List<AddressBook> addressBookList= addressBookService.list();
       return Result.success(addressBookList);
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id获取地址")
    public Result<AddressBook> getById(@PathVariable Long id){
        AddressBook addressBook= addressBookService.getById(id);
        return Result.success(addressBook);
    }
    @GetMapping("/default")
    @ApiOperation("获取默认地址")
    public Result<AddressBook> getDefault(){
        AddressBook addressBook= addressBookService.getDefault();
        return Result.success(addressBook);
    }
    @PutMapping("/default")
    @ApiOperation("获取默认地址")
    public Result<AddressBook> setDefault(@RequestBody Map<String,Long> id){
        addressBookService.setDefault(id.get("id"));
        return Result.success();
    }
    @PostMapping
    @ApiOperation("新增地址")
    public Result  save( @RequestBody AddressBook addressBook){
        addressBookService.save(addressBook);
        return Result.success();
    }
    @PutMapping
    @ApiOperation("修改地址")
    public Result update( @RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success();
    }
    @DeleteMapping
    @ApiOperation("删除地址")
    public Result delete( @RequestParam Long id){
        addressBookService.delete(id);
        return Result.success();
    }
}
